﻿// //------------------------------------------------------------------------------
// // <copyright file="" company="Dascoba Development">
// //  Copyright © Dascoba Development 2010
// // </copyright>
// //------------------------------------------------------------------------------
using System;

namespace Dascoba.Umb.FileManager.Dialogs
{
    using System.IO;

    using Dascoba.Umb.FileManager.Config;
    using Dascoba.Umb.FileManager.Support;

    /// <summary>
    ///   Upload File
    /// </summary>
    public partial class CreateFolder : umbraco.BasePages.UmbracoEnsuredPage
    {
        /// <summary>
        ///   Handles the Load event of the UploadFiles Page.
        /// </summary>
        /// <param name = "sender">The source of the event.</param>
        /// <param name = "e">The <see cref = "System.EventArgs" /> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            var folder = new DirectoryInfo(Util.MapUrl(this.Request["folder"])).Name;
            this.pane_createfolder.Text = umbraco.ui.Text("fileman", "createFolderUnder", folder, CurrentUser);
            this.pp_createfolder.Text = umbraco.ui.Text("fileman", "folderName");
            this.btnCreateFolder.Text = umbraco.ui.Text("filemanbuttons", "createFolder");
            this.closeDialog.InnerHtml = umbraco.ui.Text("filemanbuttons", "close");
        }

        /// <summary>
        ///   Handles the Click event of the btnCreateFolder control.
        /// </summary>
        /// <param name = "sender">The source of the event.</param>
        /// <param name = "e">The <see cref = "System.EventArgs" /> instance containing the event data.</param>
        protected void BtnCreateFolderClick(object sender, EventArgs e)
        {
            if (this.folderName.Text == string.Empty)
            {
                this.status.Text = umbraco.ui.Text("fileman", "folderNameRequired");
                return;
            }

            var folderUrl = this.Request["folder"] == "root"
                                ? FileManagerConfig.CurrentUserRootUrl
                                : this.Request["folder"];

            var newFolderUrl = string.Format("{0}/{1}", folderUrl, this.folderName.Text);
            DirectoryInfo directoryInfo;

            try
            {
                directoryInfo = new DirectoryInfo(Util.MapPath(newFolderUrl));
            }
            catch (Exception ex)
            {
                this.status.Text = umbraco.ui.Text("fileman", "error", ex.Message, CurrentUser);
                return;
            }

            if (directoryInfo.Exists)
            {
                this.status.Text = umbraco.ui.Text("fileman", "alreadyExists", this.folderName.Text, CurrentUser);
            }
            else
            {
                try
                {
                    directoryInfo.Create();
                    this.status.Text = umbraco.ui.Text("fileman", "wasCreated", this.folderName.Text, CurrentUser);
                    this.closeDialog.InnerText = umbraco.ui.Text("filemanbuttons", "close");
                }
                catch (Exception ex)
                {
                    this.status.Text = umbraco.ui.Text("fileman", "error", ex.Message, CurrentUser);
                }

                this.pane_createfolder.Visible = false;
            }
        }
    }
}